package com.example.demo.start;

import com.example.demo.utils.LoggerConfig;

import java.io.*;

public class StudyJavaWriterIO {

    public static void main(String[] args) {
        writerFileWithCode();
    }


    /**
     */
    private static void writerFile(){
        try {
            File file = new File("a.txt");
            if (!file.exists()){
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(new File("a.txt"));
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.append("111" + "\n");
            bufferedWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 指定编码写入
    private static void writerFileWithCode(){
        File file = new File("b.txt");
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"utf-8");
            outputStreamWriter.append("哈哈我就想知道这个怎么玩");
            outputStreamWriter.close();
            fileOutputStream.close();
        }catch (IOException e){
            LoggerConfig.getLoggerError(e.fillInStackTrace().toString());
        }

    }
}
